package com.clt.ess.gjzw;

import com.alibaba.fastjson.JSON;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.bouncycastle.util.encoders.Hex;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class testOutDataShare {
    //数据调用
    public static void testOutDataShare() {
        String catalogId = "***";
        String appKey = "***";
        String secret = "***";
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httppost = new HttpPost("http://192.168.67.200/resource/rest/v1/shared/ "+catalogId+"/1");
        //租户账号
        String username = "***";

        String nonce = UUID.randomUUID().toString();
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String created = sdf.format(new Date());
        secret = nonce + "_" + created + "_" + secret;
        byte[] md = new byte[32];
        byte[] psw = secret.getBytes();
        SM3Digest sm3 = new SM3Digest();
        sm3.update(psw, 0, psw.length);
        sm3.doFinal(md, 0);
        String passwdDigest = new String(Hex.encode(md));

        // 创建参数队列
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("App_key", appKey));
        formparams.add(new BasicNameValuePair("Username", username));
        formparams.add(new BasicNameValuePair("PasswdDigest", passwdDigest));
        formparams.add(new BasicNameValuePair("Nonce", nonce));
        formparams.add(new BasicNameValuePair("Created", created));

        Map<String, Object> param = new HashMap<>();
        param.put("***", "张三");

        formparams.add(new BasicNameValuePair("parameter",JSON.toJSONString(param)));

        UrlEncodedFormEntity uefEntity;

        try {
            uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
            httppost.setEntity(uefEntity);
            System.out.println("executing request " + httppost.getURI());
            CloseableHttpResponse response = httpClient.execute(httppost);
            try {
                HttpEntity entity = response.getEntity();
                if (entity != null) {
                    System.out.println("--------------------------------------");
                    System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8"));
                    System.out.println("--------------------------------------");
                }
            } finally {
                response.close();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接,释放资源
            try {
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
